<template>
	<view>
		<u-button type="primary" open-type="getPhoneNumber" @getphonenumber="handleLogin">获取用户号码登录</u-button>
	</view>
</template>

<script setup>
import { uniPromisify } from '../../utils/tool';
import { loginAPI } from '@/api/login';
import { useStore } from '@/store';

const { userStore } = useStore()

// 进行登录
const handleLogin = async () => {
	const loginAsync = uniPromisify(uni.login)
	
	try {
		const res = await loginAsync({
			provider: 'weixin'
		})
		
		if (!res.code) return uni.showToast({
			title: '获取登录信息失败',
			icon: 'none'
		})
		
		const data = await loginAPI(res.data)
		const token = data.token
		
		if (token) {
			userStore.setToken(token)
			uni.showToast({ title: '登录成功', icon: 'success' })
			uni.switchTab({ url: '/pages/index/index' }) // 跳转首页
		}
	} catch (error) {
		//TODO handle the exception
		uni.showToast({ title: '网络错误', icon: 'none' })
	}
}

// 获取本地用户信息
const handleGetUserProfileInfo = async () => {
	const getUserProfileAsync = uniPromisify(uni.getUserProfile)
	// 进行弹框
	
	// 获取用户信息
	try {
		const profileInfo = await getUserProfileAsync({
			desc: '用于网站登录，昵称，图片等用户信息',
			lang: 'zh_CN'
		})
		
		const userInfo = profileInfo.userInfo
	} catch (error) {
		//TODO handle the exception
	}
}
</script>

<style>

</style>
